package com.zm.test;

import com.zm.dao.IUserDao;

import com.zm.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MyBatisUser {
    private InputStream in;
    private SqlSession sqlSession;
    private IUserDao userDao;

    @Before //用于在测试方法之前执行
    public void init() throws IOException {
        //1.读取配置文件，生成字节输入流
        in= Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSessionFactory\
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
        //3.获取SqlSession对象
        sqlSession=factory.openSession(true);//事务自动提交
        //4.获取代理对象
        userDao=sqlSession.getMapper(IUserDao.class);
    }
    @After //用于在测试方法之后执行
    public  void destroy() throws IOException {
        sqlSession.close();
        in.close();
    }

    /**
     * 查询所有用户信息
     */
    @Test
    public void testFindAll() {
        List<User> users=userDao.findAll();

        for (User user : users){
            System.out.println("==========每个用户的信息============");
            System.out.println(user);
            System.out.println(user.getAccounts());
        }

    }

    /**
     * 根据id查询用户
     */
    @Test
    public void testFindOne(){
        User user =  userDao.findById(41);
        System.out.println(user);
    }




}
